<template>
  <div v-html="description"></div>
  <el-card class="box" shadow="never" :body-style="{ padding: '0' }">
    <component :is="component" class="mg-5"></component>
    <el-divider class="mg" />
    <div class="mg-5 tr">
      <el-button-group size="small">
        <el-tooltip v-for="item of operate" :key="item.text" effect="dark" :content="item.text" placement="top">
          <el-button @click="item.on" :icon="item.icon"> </el-button>
        </el-tooltip>
      </el-button-group>
    </div>
    <transition enter-active-class="enter" leave-active-class="leave">
      <div v-show="isOpen">
        <el-divider class="mg" />
        <div class="mg-5" v-html="source"></div>
        <el-divider class="mg" />
        <div class="mg-5 tc">
          <el-button @click="() => toggle(false)" icon="ArrowUp" size="small"> 收起 </el-button>
        </div>
      </div>
    </transition>
  </el-card>
</template>

<script lang="ts" setup>
import { useToggle, useClipboard } from '@vueuse/core'
import type { UseWebNotificationOptions } from '@vueuse/core'
import { ElMessage } from 'element-plus'
const props = defineProps<{
  description: string
  source: string
  rawSource: string
  path: string
}>()
const operate = [
  { text: '复制', icon: 'CopyDocument', on: () => copyCode() },
  { text: '查看源代码', icon: 'Crop', on: () => toggle() }
]
const webNotifyOpt: UseWebNotificationOptions = { dir: 'auto' }
const source = decodeURIComponent(props.source)
const description = decodeURIComponent(props.description)
const rawSource = decodeURIComponent(props.rawSource)
const component = props.path.split('/').pop()
const [isOpen, toggle] = useToggle(false)
const { copy, isSupported } = useClipboard({ source: rawSource })
function copyCode() {
  ElMessage.closeAll()
  if (!isSupported) {
    return ElMessage.error('浏览器不支持复制功能')
  }
  copy()
  ElMessage.success('复制成功')
}
</script>

<style lang="scss" scoped>
.box {
  :deep(.language-vue) {
    margin: 0;
  }
}
.enter {
  overflow: hidden;
  transform-origin: center top;
  animation: scale-y 0.3s ease-out;
}
.leave {
  overflow: hidden;
  transform-origin: center top;
  animation: scale-y 0.3s ease-out reverse;
}

@keyframes scale-y {
  0% {
    max-height: 0vh;
  }
  100% {
    max-height: 100vh;
  }
}
</style>
